# 主板信息
from lib.config.settings import settings
import os
class Board:
    def process(self, func_name):
        if not settings.DEBUG:  # 测试环境
            BASE_DIR = settings.BASE_DIR
            path = os.path.join(BASE_DIR, r'files\board.out')
            fp = open(r'%s' % path, 'r', encoding='utf-8')
            info = fp.read()
            return self.parse(info)

        content = func_name('sudo dmidecode -t1')
        return self.parse(content)

    def parse(self, content):
        result = {}
        key_map = {
            # 制造商
            'Manufacturer': 'manufacturer',
            # 主板名字
            'Product Name': 'model',
            # 主板型号
            'Serial Number': 'sn',
        }

        for item in content.split('\n'):
            row_data = item.strip().split(':')
            if len(row_data) == 2:
                if row_data[0] in key_map:
                    result[key_map[row_data[0]]] = row_data[1].strip() if row_data[1] else row_data[1]
        return result
